<template>
  <div>
    <Tabs :animated="false"
          type="card"
          value="1"
          @on-click="changeList">
      <TabPane label="公司新闻"
               :name="'1'">
        <Table :columns="columns"
               :data="newsListData"></Table>
      </TabPane>
      <TabPane label="行业新闻"
               :name="'0'">
        <Table :columns="columns"
               :data="newsListData"></Table>
      </TabPane>
    </Tabs>
    <div style="margin: 10px;overflow: hidden">
      <div style="float: right;">
        <Page :total="total"
              :current="searchForm.pageNum"
              @on-change="changePage"></Page>
      </div>
    </div>
  </div>
</template>

<script>
import server from '@/api/news';

export default {
  data () {
    return {
      total: 0,
      columns: [
        {
          title: "新闻标题",
          key: "articleTitle"
        },
        {
          title: "新闻作者",
          key: "newsAutor"
        },
        {
          title: "发布时间",
          key: "publishTime"
        },
        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          width: 160,
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'text',
                  size: 'small'
                },
                on: {
                  click: () => {
                    Object.keys(params.row).map(key => {
                      localStorage[key] = params.row[key];
                    })
                    this.$router.push({ path: 'newslist/newsedit', query: { handle: 'edit' } });
                  }
                }
              }, '修改'),
              h('Poptip', {
                props: {
                  confirm: true,
                  title: '确认删除吗？',
                },
                on: {
                  'on-ok': async () => {
                    const res = await server.remove(params.row._id);
                    if (!res.code) {
                      this.$message.success('删除成功！');
                      this.getData();
                    }
                  }
                },
              },
                [
                  h('Button', {
                    props: {
                      type: 'text',
                      size: 'small'
                    }
                  }, '删除')
                ]),
            ]);
          }
        }
      ],
      newsListData: [],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        data: {
          newsType: 1, // 新闻类型 1：公司新闻 0：行业新闻
        }
      }
    };
  },
  created () {
    this.getData();
  },
  methods: {
    async getData () {
      let res = await server.list(this.searchForm);
      if (!res.code) {
        this.newsListData = res.data.items;
        this.total = res.data.total;
      }
    },
    // 切换新闻类型
    changeList (newsType) {
      this.searchForm.pageNum = 1;
      this.searchForm.data.newsType = newsType;
      this.getData();
    },
    // 分页
    changePage (pageNum) {
      this.searchForm.pageNum = pageNum;
      this.getData();
    }
  }
};
</script>

<style>
</style>
